package com.redwine.app.fragments;

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.redwine.app.utils.ShowDialogUtils;
import com.redwine.app.view.EmptyView;

/**
 * 作者：gassion on 17/4/4 18:15
 * 最可怕的敌人，就是没有坚强的信念。
 */

public class BaseFragment extends Fragment {

    private ProgressDialog mProgressDialog;

    /**
     * 点击加载错误的回调
     */
    protected void emptyOnclick() {

    }

    /**
     * 设置网络错误展示
     */
    public void setErrorView(EmptyView emptyView, PullToRefreshListView mListView) {
        emptyView.show();
        if (null != mListView)
            mListView.setMode(PullToRefreshBase.Mode.DISABLED);
        emptyView.emptyTopLayout.setVisibility(View.VISIBLE);
        emptyView.emptyErrorBottomImage.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        emptyView.setEnabled(true);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyOnclick();
            }
        });
    }

    /**
     * 设置empty展示
     */
    public void setEmptyView(EmptyView emptyView, PullToRefreshListView mListView) {
        emptyView.show();
        if (null != mListView)
            mListView.setMode(PullToRefreshBase.Mode.DISABLED);
        emptyView.setVisibility(View.VISIBLE);
        emptyView.setEnabled(true);
        emptyView.emptyTopLayout.setVisibility(View.GONE);
        emptyView.emptyErrorBottomImage.setVisibility(View.VISIBLE);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyOnclick();
            }
        });
    }

    /**
     * 设置隐藏展示的view
     */
    public void setHiddenView(EmptyView emptyView) {
        emptyView.bind();
        emptyView.setVisibility(View.GONE);
        emptyView.emptyTopLayout.setVisibility(View.GONE);
        emptyView.emptyErrorBottomImage.setVisibility(View.VISIBLE);
        emptyView.setEnabled(false);
    }

    public void showWaiting() {
        if (mProgressDialog == null) {
            mProgressDialog = ShowDialogUtils.loadingDialog(getActivity());
        } else {
            mProgressDialog.show();
        }
    }

    public void dismissWaiting() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * 设置更新时间
     */
    protected void setUpdateTime(PullToRefreshListView mListView) {
        String label = DateUtils.formatDateTime(getActivity(),
                System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                        | DateUtils.FORMAT_SHOW_DATE
                        | DateUtils.FORMAT_ABBREV_ALL);
        mListView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
    }
}
